使用 bind 确保this的上下文
const localCacheGetItem = cacheStore.get
const localCacheSetItem = cacheStore.set
如上代码,cacheStore
是工厂函数的实例,在 cacheStore
内部有一个 cache
对象,当调用set/get
时,内部调用的是this.cache
进行赋值/获取。直接将 cacheStore.get
赋值给 localCacheGetItem
,调用 localCacheGetItem
会出现 this
为 undefined的
情况,在这种情况下,应该是要使用 bind
改写。
在JavaScript中,当你将一个对象的方法赋值给一个变量,然后调用这个变量时,这个方法的上下文(this
)会丢失。你可以使用 bind
方法来确保 this
的上下文。以下是修改后的代码:
const localCacheGetItem = cacheStore.get.bind(cacheStore);
const localCacheSetItem = cacheStore.set.bind(cacheStore);
在这个版本的代码中,我们使用 bind
方法将 cacheStore.get
和 cacheStore.set
的上下文绑定到 cacheStore
。这样,无论你如何调用 localCacheGetItem
和 localCacheSetItem
,它们的 this
始终指向 cacheStore
。